package com.unknown.wificontrol.ui.home;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.unknown.wificontrol.R;
import com.unknown.wificontrol.http.Http;
import com.unknown.wificontrol.http.HttpListener;
import com.unknown.wificontrol.listener.HXItemSelectListener;
import com.unknown.wificontrol.listener.OnDialogListener;
import com.unknown.wificontrol.ui.BaseActivity;
import com.unknown.wificontrol.util.Preference;
import com.unknown.wificontrol.widget.BottomPopItemView;

import org.json.JSONObject;

import butterknife.BindView;

public class GainSettingActivity extends BaseActivity {
    @Override
    public int getLayout() {
        return R.layout.layout_gain_setting;
    }

    @Override
    public void initViews() {
        etLNAGain.setText(Preference.get(this, "LNAGain", "15.6"));
        etLNAGain.setSelection(etLNAGain.getText().toString().length());
        etPGAGain.setText(Preference.get(this, "PGAGain", "21"));
        etVGAGain.setText(Preference.get(this, "VGAGain", "-42"));

        etLNAGain.addTextChangedListener(watcher);
        etPGAGain.addTextChangedListener(watcher);
        etVGAGain.addTextChangedListener(watcher);

        updateAllGain();
    }

    @BindView(R.id.etLNAGain)
    EditText etLNAGain;

    @BindView(R.id.etPGAGain)
    EditText etPGAGain;

    @BindView(R.id.etVGAGain)
    EditText etVGAGain;

    @BindView(R.id.etAllGain)
    EditText etAllGain;


    public void onItem(View v) {
        switch (v.getId()) {
            case R.id.ivLNAGain: {
                BottomPopItemView item = new BottomPopItemView(mActivity);
                final String[] items = new String[]{"15,6", "17.9", "21.3"};
                item.show(etLNAGain.getText().toString(), items, new HXItemSelectListener() {
                    @Override
                    public void onSelected(int index) {
                        etLNAGain.setText(items[index]);
                        etLNAGain.setSelection(etLNAGain.getText().toString().length());
                    }
                });
            }
            break;
            case R.id.ivPGAGain: {
                BottomPopItemView item = new BottomPopItemView(mActivity);
                final String[] items = new String[]{"21", "24", "27", "30"};
                item.show(etPGAGain.getText().toString(), items, new HXItemSelectListener() {
                    @Override
                    public void onSelected(int index) {
                        etPGAGain.setText(items[index]);
                        etPGAGain.setSelection(etPGAGain.getText().toString().length());
                    }
                });
            }
            break;
            case R.id.ivVGAGain: {
                BottomPopItemView item = new BottomPopItemView(mActivity);
                final String[] items = new String[]{"-42.0", "-40.0", "-35.0", "-30.0", "-25.0", "-20.0", "-10.0", "0.0"};
                item.show(etVGAGain.getText().toString(), items, new HXItemSelectListener() {
                    @Override
                    public void onSelected(int index) {
                        etVGAGain.setText(items[index]);
                        etVGAGain.setSelection(etVGAGain.getText().toString().length());
                    }
                });
            }
            break;
        }
    }

    public void onCommit(View v) {
        String LNAGain = etLNAGain.getText().toString();
        String PGAGain = etPGAGain.getText().toString();
        String VGAGain = etVGAGain.getText().toString();
        String AllGain = etAllGain.getText().toString();
        if (isEmpty(LNAGain)) {
            showToast("请输入LNA增益");
            return;
        }
        if (isEmpty(PGAGain)) {
            showToast("请输入PGA增益");
            return;
        }
        if (isEmpty(VGAGain)) {
            showToast("请输入VGA增益");
            return;
        }

        if (isEmpty(AllGain)) {
            showToast("请输入总增益");
            return;
        }

        JSONObject object = new JSONObject();
        try {
            object.put("LNAGain", LNAGain);
            object.put("PGAGain", PGAGain);
            object.put("VGAGain", VGAGain);
            object.put("AllGain", AllGain);
        } catch (Exception exception) {

        }
        showProgress("设置中...");
        Http.post(getHost() + "/getValues/Gain", object.toString(), new HttpListener() {
            @Override
            public void onReturn(JSONObject jsonObject) {
                hideProgress();
                if (jsonObject != null && jsonObject.length() > 0) {
                    int code = jsonObject.optInt("code");
                    String message = jsonObject.optString("message");
                    if (code == 200) {
                        Preference.save(mActivity, "LNAGain", LNAGain);
                        Preference.save(mActivity, "PGAGain", PGAGain);
                        Preference.save(mActivity, "VGAGain", VGAGain);
                        Preference.save(mActivity, "AllGain", AllGain);
                        showSuccess("增益设置成功", new OnDialogListener() {
                            @Override
                            public void onConfirm() {
                                super.onConfirm();
                                finish();
                            }
                        });
                    } else {
                        showToast(message);
                    }
                } else {
                    showToast("请求失败");
                }

            }
        });
    }

    private TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            updateAllGain();
        }
    };

    private void updateAllGain() {
        float LNAGain = 0;
        float PGAGain = 0;
        float VGAGain = 0;

        try {
            String LNAGainStr = etLNAGain.getText().toString();
            String PGAGainStr = etPGAGain.getText().toString();
            String VGAGainStr = etVGAGain.getText().toString();
            if (!isEmpty(LNAGainStr)) {
                LNAGain = Float.parseFloat(LNAGainStr);
            }
            if (!isEmpty(PGAGainStr)) {
                PGAGain = Float.parseFloat(PGAGainStr);
            }
            if (!isEmpty(VGAGainStr)) {
                VGAGain = Float.parseFloat(VGAGainStr);
            }
        } catch (Exception ex) {

        }

        float allGain = LNAGain + PGAGain + VGAGain;
        etAllGain.setText(String.format("%.2f", allGain));
    }
}
